// 主题状态管理 slice
import { createSlice, type PayloadAction } from '@reduxjs/toolkit';

// 主题类型定义
export type Theme = 'dark' | 'light';

// 主题状态接口
interface ThemeState {
  currentTheme: Theme;
}

// 初始状态 - 默认使用深色主题
const initialState: ThemeState = {
  currentTheme: 'dark',
};

// 创建主题 slice
const themeSlice = createSlice({
  name: 'theme', // slice 名称
  initialState, // 初始状态
  reducers: {
    // 切换主题动作
    toggleTheme: (state) => {
      // 在深色和浅色主题之间切换
      state.currentTheme = state.currentTheme === 'dark' ? 'light' : 'dark';
    },
    // 设置特定主题动作
    setTheme: (state, action: PayloadAction<Theme>) => {
      state.currentTheme = action.payload;
    },
  },
});

// 导出动作创建器
export const { toggleTheme, setTheme } = themeSlice.actions;

// 导出 reducer
export default themeSlice.reducer;
